using ERPBackend.Data;
using ERPBackend.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace ERPBackend.Services
{
    public class SupplierContactService : ISupplierContactService
    {
        private readonly ApplicationDbContext _context;

        public SupplierContactService(ApplicationDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public async Task<IEnumerable<SupplierContact>> GetContactsBySupplierIdAsync(int supplierId)
        {
            return await _context.SupplierContacts
                .Where(sc => sc.SupplierId == supplierId)
                .ToListAsync() ?? Enumerable.Empty<SupplierContact>();
        }

        public async Task<SupplierContact?> GetContactByIdAsync(int id)
        {
            return await _context.SupplierContacts.FindAsync(id);
        }

        public async Task<SupplierContact> CreateContactAsync(SupplierContact contact)
        {
            // 检查供应商是否存在
            var supplierExists = await _context.Suppliers
                .AnyAsync(s => s.Id == contact.SupplierId);
            if (!supplierExists)
            {
                throw new ArgumentException("供应商不存在");
            }

            _context.SupplierContacts.Add(contact);
            await _context.SaveChangesAsync();
            return contact;
        }

        public async Task<bool> UpdateContactAsync(SupplierContact contact)
        {
            // 检查联系人是否存在
            var existingContact = await _context.SupplierContacts.FindAsync(contact.Id);
            if (existingContact == null)
            {
                return false;
            }

            // 检查供应商是否存在
            var supplierExists = await _context.Suppliers
                .AnyAsync(s => s.Id == contact.SupplierId);
            if (!supplierExists)
            {
                throw new ArgumentException("供应商不存在");
            }

            // 更新属性
            existingContact.ContactType = contact.ContactType;
            existingContact.ContactName = contact.ContactName;
            existingContact.Phone = contact.Phone;
            existingContact.Email = contact.Email;
            existingContact.Notes = contact.Notes;

            _context.Entry(existingContact).State = EntityState.Modified;
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<bool> DeleteContactAsync(int id)
        {
            var contact = await _context.SupplierContacts.FindAsync(id);
            if (contact == null)
            {
                return false;
            }

            _context.SupplierContacts.Remove(contact);
            await _context.SaveChangesAsync();
            return true;
        }

        // 删除SetPrimaryContactAsync方法，因为不再需要主要联系人功能
        // public async Task<bool> SetPrimaryContactAsync(int supplierId, int contactId)
        // {
        //     // 检查联系人是否存在
        //     var contact = await _context.SupplierContacts
        //         .FirstOrDefaultAsync(sc => sc.Id == contactId && sc.SupplierId == supplierId);
        //     if (contact == null)
        //     {
        //         return false;
        //     }

        //     // 取消其他联系人的主要状态
        //     await ResetPrimaryContactsAsync(supplierId);

        //     // 设置当前联系人为主要联系人
        //     contact.IsPrimary = 1;
        //     _context.Entry(contact).State = EntityState.Modified;
        //     await _context.SaveChangesAsync();
        //     return true;
        // }

        // 删除ResetPrimaryContactsAsync方法，因为不再需要主要联系人功能
        // private async Task ResetPrimaryContactsAsync(int supplierId)
        // {
        //     var primaryContacts = await _context.SupplierContacts
        //         .Where(sc => sc.SupplierId == supplierId && sc.IsPrimary == 1)
        //         .ToListAsync();

        //     foreach (var primaryContact in primaryContacts)
        //     {
        //         primaryContact.IsPrimary = 0;
        //         _context.Entry(primaryContact).State = EntityState.Modified;
        //     }

        //     await _context.SaveChangesAsync();
        // }
    }
}